﻿#region Copyright (c) 11/27/2011, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Reflection;
using NUnit.Framework;


namespace Amarok.Presentation
{
	[TestFixture]
	public class Test_ViewActionHandlerInspector
	{
		private class MyViewAction : ViewAction
		{
		}

		private abstract class TestBaseClass
		{
			public abstract Boolean Method2(MyViewAction information);

			[ViewActionHandler]
			public abstract Boolean Method3(MyViewAction information);
		}

		private class TestClass : TestBaseClass
		{
			public Boolean Method1(MyViewAction information)
			{
				// no attribute
				return true;
			}

			[ViewActionHandler]
			public override Boolean Method2(MyViewAction information)
			{
				// abstract method with attribute
				return true;
			}

			public override Boolean Method3(MyViewAction information)
			{
				// abstract method with attribute derived
				return true;
			}

			[ViewActionHandler]
			public Boolean Method4<T>(MyViewAction information)
			{
				// generic method
				return true;
			}

			[ViewActionHandler]
			public Boolean Method5()
			{
				// no parameters
				return true;
			}

			[ViewActionHandler]
			public Boolean Method6(MyViewAction information, Object dummy)
			{
				// too many parameters
				return true;
			}

			[ViewActionHandler]
			public Boolean Method7(Object information)
			{
				// invalid parameter type
				return true;
			}

			[ViewActionHandler]
			public String Method8(MyViewAction information)
			{
				// invalid return type
				return null;
			}

			[ViewActionHandler]
			public Boolean Method9(ViewAction information)
			{
				return true;
			}
		}


		private MethodInfo GetMethodInfo(String name)
		{
			var target = new TestClass();
			return target.GetType().GetMethod(name);
		}

		private HandlerInfo InspectMethod(String name)
		{
			var methodInfo = GetMethodInfo(name);
			var inspector = new ViewActionHandlerInspector();
			return inspector.Inspect(methodInfo);
		}


		// --- TESTS ---


		[Test]
		public void Test_Inspect_NoAttribute()
		{
			var handlerInfo = InspectMethod("Method1");
			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_AbstractMethod_DerivedMethodWithAttribute()
		{
			var handlerInfo = InspectMethod("Method2");
			Assert.That(handlerInfo, Is.Not.Null);
		}

		[Test]
		public void Test_Inspect_AbstractMethod_BaseMethodWithAttribute()
		{
			var handlerInfo = InspectMethod("Method3");
			Assert.That(handlerInfo, Is.Not.Null);
		}

		[Test]
		public void Test_Inspect_GenericMethod()
		{
			var handlerInfo = InspectMethod("Method4");
			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_NoParameter()
		{
			var handlerInfo = InspectMethod("Method5");
			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_TwoParameters()
		{
			var handlerInfo = InspectMethod("Method6");
			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_ParameterNotInformationType()
		{
			var handlerInfo = InspectMethod("Method7");
			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_ReturnTypeNotVoid()
		{
			var handlerInfo = InspectMethod("Method8");
			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_ViewActionBaseType()
		{
			var handlerInfo = InspectMethod("Method9");
			Assert.That(handlerInfo, Is.Not.Null);
		}

	}
}

#endif
